package common.inter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.handler.Handler;

import common.kit.PropKits;

public class CorsHandler extends Handler{

	@Override
	public void handle(String target, HttpServletRequest request, HttpServletResponse response, boolean[] isHandled) {
		if (PropKits.isDevMode()) {
			response.setHeader("Access-Control-Allow-Origin", "*");
		}
		response.addHeader("Access-Control-Allow-Credentials", "true");
		response.addHeader("Access-Control-Allow-Methods","POST,GET,OPTIONS");
		response.addHeader("Access-Control-Allow-Headers","*");  //使用通配符
		response.setHeader("Access-Control-Max-Age", "3600");
		
		if ("OPTIONS".equals(request.getMethod())) {
			isHandled[0] = true;
			response.setStatus(HttpServletResponse.SC_OK);
			return ;
		}
		
		next.handle(target, request, response, isHandled);
	}

}
